package com.dizner.diassistant.ui.notifications

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.bin.david.form.core.SmartTable
import com.dizner.diassistant.R
import com.dizner.diassistant.database.AppDatabase
import com.dizner.diassistant.database.DBOptionManager
import com.dizner.diassistant.dataentry.BloodPressureHistoryEntry
import kotlinx.android.synthetic.main.fragment_notifications.*

class NotificationsFragment : Fragment() {

    private lateinit var notificationsViewModel: NotificationsViewModel

    private lateinit var appDatabase: AppDatabase

    private lateinit var mTable :SmartTable<BloodPressureHistoryEntry>

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        notificationsViewModel =
            ViewModelProviders.of(this).get(NotificationsViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_notifications, container, false)
        appDatabase = AppDatabase.getInstance(context!!)
        mTable = root.findViewById<SmartTable<BloodPressureHistoryEntry>>(R.id.table_history)
        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initData()
    }

    private fun initData() {
        queryAllData()
    }

    private fun queryAllData() {
        DBOptionManager.instance.executeDBOption(appDatabase.getHistoryDao().getAllHistory(),
            object :
                DBOptionManager.Callback<List<BloodPressureHistoryEntry>>() {
                override fun onSuccess(data: List<BloodPressureHistoryEntry>?) {
                    btn_clear_data.isEnabled = data?.size != 0

                    mTable.setData(data)
                }

                override fun onFailure(error: Throwable) {
                    toast("没有数据")
                }
            })
    }

    private fun initView() {
        btn_clear_data.setOnClickListener {
            DBOptionManager.instance.executeDBOption(appDatabase.getHistoryDao().getAllHistory(),
                object :
                    DBOptionManager.Callback<List<BloodPressureHistoryEntry>>() {
                    override fun onSuccess(data: List<BloodPressureHistoryEntry>?) {
                        clearAllData(data)
                    }

                    override fun onFailure(error: Throwable) {
                        toast("没有数据")
                    }
                })
        }

        btn_init_data.setOnClickListener {



            DBOptionManager.instance.executeDBOption(appDatabase.getHistoryDao().getAllHistory(),
                object :
                    DBOptionManager.Callback<List<BloodPressureHistoryEntry>>() {
                    override fun onSuccess(data: List<BloodPressureHistoryEntry>?) {
                        DBOptionManager.instance.executeDBOption(
                            appDatabase.getHistoryDao().clearHistory(
                                data!!
                            ), object : DBOptionManager.Callback<Int>() {
                                override fun onSuccess(data: Int?) {
                                    var datas = DBOptionManager.instance.initData()

                                    for (data in datas) {
                                        DBOptionManager.instance.executeDBOption(
                                            appDatabase.getHistoryDao().addNewHistory(
                                                data
                                            ), object : DBOptionManager.Callback<Long>() {
                                                override fun onSuccess(data: Long?) {
                                                    toast("初始化成功")
                                                }

                                                override fun onFailure(error: Throwable) {
                                                    toast("清理失败")
                                                }
                                            })
                                    }
                                }

                                override fun onFailure(error: Throwable) {
                                    toast("清理失败")
                                }
                            })

                    }

                    override fun onFailure(error: Throwable) {
                        toast("没有数据")
                        var datas = DBOptionManager.instance.initData()

                        for (data in datas) {
                            DBOptionManager.instance.executeDBOption(
                                appDatabase.getHistoryDao().addNewHistory(
                                    data
                                ), object : DBOptionManager.Callback<Long>() {
                                    override fun onSuccess(data: Long?) {
                                        toast("初始化成功")
                                    }

                                    override fun onFailure(error: Throwable) {
                                        toast("清理失败")
                                    }
                                })
                        }
                    }
                })

        }
    }

    private fun clearAllData(data: List<BloodPressureHistoryEntry>?) {
        DBOptionManager.instance.executeDBOption(
            appDatabase.getHistoryDao().clearHistory(
                data!!
            ), object : DBOptionManager.Callback<Int>() {
                override fun onSuccess(data: Int?) {
                    toast("已删除{$data}条数据")
                }

                override fun onFailure(error: Throwable) {
                    toast("清理失败")
                }
            })
    }

    private fun toast(toastMsg: String) {
        Toast.makeText(context, toastMsg + "", Toast.LENGTH_SHORT).show()
    }
}